#include "./../inc/game_page.h"

GamePage *GamePage::m_instance = NULL;

GamePage *GamePage::instance(Snake *snake, Lcd *lcd, ShowFont *showFont, GameManager *gameManager)
{
    // 如果没有实例则创建实例
    if (m_instance == NULL)
    {
        m_instance = new GamePage(snake, lcd, showFont, gameManager);
    }
    return m_instance;
}

void GamePage::delInstance()
{
    // 释放实例空间
    delete m_instance;
    m_instance = NULL;
}

// 触摸事件处理实现
bool GamePage::handleTouch(const Point &touch_point)
{
    // 功能栏及方向键坐标定义
    static const int TOOLBAR_X_START = 0;
    static const int TOOLBAR_X_END = 200;
    static const int TOOLBAR_Y_START = 0;
    static const int TOOLBAR_Y_END = 480;

    // 上键：位置(70,270)，尺寸60*80（宽*高）
    static const int UP_KEY_X = 70;
    static const int UP_KEY_Y = 270;
    static const int UP_KEY_WIDTH = 60;
    static const int UP_KEY_HEIGHT = 80;

    // 下键：位置(70,390)，尺寸60*80
    static const int DOWN_KEY_X = 70;
    static const int DOWN_KEY_Y = 390;
    static const int DOWN_KEY_WIDTH = 60;
    static const int DOWN_KEY_HEIGHT = 80;

    // 左键：位置(0,340)，尺寸80*60
    static const int LEFT_KEY_X = 0;
    static const int LEFT_KEY_Y = 340;
    static const int LEFT_KEY_WIDTH = 80;
    static const int LEFT_KEY_HEIGHT = 60;

    // 右键：位置(120,340)，尺寸80*60
    static const int RIGHT_KEY_X = 120;
    static const int RIGHT_KEY_Y = 340;
    static const int RIGHT_KEY_WIDTH = 80;
    static const int RIGHT_KEY_HEIGHT = 60;

    int touch_x = touch_point.x();
    int touch_y = touch_point.y();

    // 判断是否在功能栏范围内
    if (touch_x < TOOLBAR_X_START || touch_x > TOOLBAR_X_END ||
        touch_y < TOOLBAR_Y_START || touch_y > TOOLBAR_Y_END)
    {
        return false;
    }

    if (touch_x >= 0 && touch_x < 60 && touch_y >= 0 && touch_y < 60)
    {
        gameManager->showMenuPopup();
        return true;
    }

    Dir dir = NONE;

    // 上键区域判断
    if (touch_x >= UP_KEY_X && touch_x < UP_KEY_X + UP_KEY_WIDTH &&
        touch_y >= UP_KEY_Y && touch_y < UP_KEY_Y + UP_KEY_HEIGHT)
    {
        dir = UP;
    }
    // 下键区域判断
    else if (touch_x >= DOWN_KEY_X && touch_x < DOWN_KEY_X + DOWN_KEY_WIDTH &&
             touch_y >= DOWN_KEY_Y && touch_y < DOWN_KEY_Y + DOWN_KEY_HEIGHT)
    {
        dir = DOWN;
    }
    // 左键区域判断
    else if (touch_x >= LEFT_KEY_X && touch_x < LEFT_KEY_X + LEFT_KEY_WIDTH &&
             touch_y >= LEFT_KEY_Y && touch_y < LEFT_KEY_Y + LEFT_KEY_HEIGHT)
    {
        dir = LEFT;
    }
    // 右键区域判断
    else if (touch_x >= RIGHT_KEY_X && touch_x < RIGHT_KEY_X + RIGHT_KEY_WIDTH &&
             touch_y >= RIGHT_KEY_Y && touch_y < RIGHT_KEY_Y + RIGHT_KEY_HEIGHT)
    {
        dir = RIGHT;
    }

    // 设置蛇的方向
    if (dir != NONE && snake != nullptr)
    {
        snake->setDirection(dir);
        return true;
    }

    return false;
}

void GamePage::toolbarInit()
{
    // 显示工具栏
    BMP toolbar("./res/toolbar.bmp");
    toolbar.show(lcd->addr());
    // BMP direction("./res/direction.bmp");
    // direction.show(lcd->addr(), 0, 270);
    BMP menu("./res/menu.bmp");
    menu.show(lcd->addr(), 10, 10);
}

void GamePage::showInfo()
{
    // 获取当前游戏的等级、分数和速度
    int level = gameManager->getLevel();
    int score = gameManager->getScore();
    int speed = gameManager->getSpeed();

    // 初始化用于存储格式化字符串的缓冲区
    char level_str[20] = {0};
    char score_str[20] = {0};
    char speed_str[20] = {0};

    // 根据游戏等级设置难度描述字符串
    if (level == 0)
    {
        sprintf(level_str, "难度: 简单");
    }
    else if (level == 1)
    {
        sprintf(level_str, "难度: 普通");
    }
    else if (level == 2)
    {
        sprintf(level_str, "难度: 困难");
    }

    sprintf(score_str, "评分: %d", score);
    sprintf(speed_str, "速度: %d", speed);

    showFont->display(
        (char *)level_str,
        30,
        120,
        30,
        ShowFont::black,
        ShowFont::red,
        40,
        90);

    showFont->display(
        (char *)score_str,
        30,
        120,
        30,
        ShowFont::black,
        ShowFont::red,
        40,
        124);

    showFont->display(
        (char *)speed_str,
        30,
        120,
        30,
        ShowFont::black,
        ShowFont::red,
        40,
        158);

    up_w.showIcon(lcd->addr(), 70, 270);
    down_w.showIcon(lcd->addr(), 70, 390);
    left_w.showIcon(lcd->addr(), 0, 340);
    right_w.showIcon(lcd->addr(), 120, 340);

    switch (snake->getDirection())
    {
    case LEFT:

        left_b.showIcon(lcd->addr(), 0, 340);
        break;
    case RIGHT:
        right_b.showIcon(lcd->addr(), 120, 340);
        break;
    case UP:
        up_b.showIcon(lcd->addr(), 70, 270);
        break;
    case DOWN:
        down_b.showIcon(lcd->addr(), 70, 390);
    }
}

void GamePage::showGameOver()
{
    BMP game_over("./res/gameover.bmp");
    game_over.showIcon(lcd->addr(), 300, 0);
}

// 显示页面实现
void GamePage::show()
{
    toolbarInit();
    showInfo();
}

// 隐藏页面实现
void GamePage::hide()
{
    // 隐藏游戏页面元素的具体实现
    // 例如：清除页面绘制内容等
}